package com.ruoyi.web.controller.core;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.ruoyi.core.domain.Codes;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.core.domain.UserBanner;
import com.ruoyi.core.service.IUserBannerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 轮播图列表Controller
 * 
 * @author lgc
 * @date 2020-08-17
 */
@Controller
@RequestMapping("/core/banner")
public class UserBannerController extends BaseController
{
    private String prefix = "core/banner";

    @Autowired
    private IUserBannerService userBannerService;

    @RequiresPermissions("core:banner:view")
    @GetMapping()
    public String banner()
    {
        return prefix + "/banner";
    }

    /**
     * 排序号是否重复
     */
    @PostMapping( "/getBannerSequence")
    @ResponseBody
    public Boolean  getStartCode(Long bannerSequence){
        //是否存在
        Boolean flag = userBannerService.checkBannerSequence(bannerSequence,ShiroUtils.getUserId());
        return flag;
    }

    /**
     * 查询轮播图列表列表
     */
    @RequiresPermissions("core:banner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserBanner userBanner)
    {
        Long userId = ShiroUtils.getUserId();
        userBanner.setUserId(userId);
        startPage();
        List<UserBanner> list = userBannerService.selectUserBannerList(userBanner);
        return getDataTable(list);
    }

    /**
     * 导出轮播图列表列表
     */
    @RequiresPermissions("core:banner:export")
    @Log(title = "轮播图列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserBanner userBanner)
    {
        List<UserBanner> list = userBannerService.selectUserBannerList(userBanner);
        ExcelUtil<UserBanner> util = new ExcelUtil<UserBanner>(UserBanner.class);
        return util.exportExcel(list, "banner");
    }

    /**
     * 新增轮播图列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存轮播图列表
     */
    @RequiresPermissions("core:banner:add")
    @Log(title = "轮播图列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserBanner userBanner)
    {
        userBanner.setUserId(ShiroUtils.getUserId());
        userBanner.setCreatedDate(new Date());
        userBanner.setCreatedBy(ShiroUtils.getUserId());
        return toAjax(userBannerService.insertUserBanner(userBanner));
    }

    /**
     * 修改轮播图列表
     */
    @GetMapping("/edit/{bannerId}")
    public String edit(@PathVariable("bannerId") Long bannerId, ModelMap mmap)
    {
        UserBanner userBanner = userBannerService.selectUserBannerById(bannerId);
        mmap.put("userBanner", userBanner);
        return prefix + "/edit";
    }

    /**
     * 修改保存轮播图列表
     */
    @RequiresPermissions("core:banner:edit")
    @Log(title = "轮播图列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserBanner userBanner)
    {
        return toAjax(userBannerService.updateUserBanner(userBanner));
    }

    /**
     * 删除轮播图列表
     */
    @RequiresPermissions("core:banner:remove")
    @Log(title = "轮播图列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(userBannerService.deleteUserBannerByIds(ids));
    }
}
